package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @ClassName: Employee
 * @Author: zw
 * @Description: 出百万拳，行万里路；
 * @Create 2024/8/24-21:54
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    @ApiOperation(value = "客户端登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("请求员工参数：{}", employeeLoginDTO);
        Employee employeeInfo = employeeService.login(employeeLoginDTO);
        // 获取到employeeInfo 信息后，用jwtutile 生产token并返回
        HashMap<String, Object> clamis = new HashMap<>();
        clamis.put(JwtClaimsConstant.EMP_ID, employeeInfo.getId());
        // 密钥—+ 过期时间+ hashMap（id， userId）；
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), clamis);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .userName(employeeInfo.getUsername())
                .id(employeeInfo.getId())
                .name(employeeInfo.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }


    @ApiOperation(value = "新增员工")
    @PostMapping
    public Result  save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工，{}", employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "客户员工退出")
    public  Result<String> logout () {
        return Result.success();
    }
}
